package com.dy.lock.screen.api.utils;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.dy.lock.screen.api.constants.Constant;
import com.dy.lock.screen.api.utils.IpUtil;
import com.dy.lock.screen.api.vo.UserResultVo;

public class ServletUtil extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static Logger log = Logger.getLogger(HttpServlet.class);

	@SuppressWarnings("unchecked")
	public static void logReqParams(HttpServletRequest req, String requestId) {
		Map map = req.getParameterMap();
		Set set = map.keySet();
		Iterator ita = set.iterator();
		StringBuilder sb = new StringBuilder();
		String reqParam = null;
		String[] valueArr = null;
		while (ita.hasNext()) {
			reqParam = (String) ita.next();
			sb.append(reqParam).append(":");
			if (null != map.get(reqParam)) {
				valueArr = (String[]) map.get(reqParam);
				if (valueArr != null && valueArr.length > 0) {
					sb.append(valueArr[0]).append("|");
				}
			}
		}
		log.info("requestId[" + requestId + "]请求:[" + sb.toString() + "]"
				+ "来源ip:[" + IpUtil.getIpAddr(req) + "]");
	}

	public static void writeResXMLStr(HttpServletResponse resp, String resStr,
			String requestId) {
		PrintWriter out = null;
		try {
			resp.setCharacterEncoding("UTF-8");
			resp.setContentType("text/xml;charset=utf-8");
			resp.setHeader("Cache-Control", "no-cache");
			out = resp.getWriter();
			out.write(resStr);
			log.info("requestId[" + requestId + "]响应:[" + resStr + "]");
		} catch (IOException e) {
			log.error("http响应异常,requestId=[" + requestId + "]", e);
		} finally {
			if (out != null) {
				out.flush();
				out.close();
			}
		}
	}

	public static void writeResJsonStr(HttpServletResponse resp, String resStr) {
		PrintWriter out = null;
		try {
			resp.setCharacterEncoding("UTF-8");
			resp.setContentType("application/json;charset=utf-8");
			resp.setHeader("Cache-Control", "no-cache");
			out = resp.getWriter();
			out.write(resStr);
			log.info("响应:[" + resStr + "]");
		} catch (IOException e) {
			log.error("http响应异常", e);
		} finally {
			if (out != null) {
				out.flush();
				out.close();
			}
		}
	}

	public static void writeResHtmlStr(HttpServletResponse resp, String resStr,
			String requestId) {
		PrintWriter out = null;
		try {
			resp.setCharacterEncoding("UTF-8");
			resp.setContentType("text/html;charset=utf-8");
			resp.setHeader("Cache-Control", "no-cache");
			out = resp.getWriter();
			out.write(resStr);
			log.info("requestId[" + requestId + "]响应:[" + resStr + "]");
		} catch (IOException e) {
			log.error("http响应异常,requestId=[" + requestId + "]", e);
		} finally {
			if (out != null) {
				out.flush();
				out.close();
			}
		}
	}

	/**
	 * 统一定义异常信息
	 * 
	 * @param resp
	 */
	public static void writeErrorStr(HttpServletResponse resp) {
		UserResultVo ri = new UserResultVo();
		ri.setCode(Constant.UNKNOW_ERROR);
		ri.setStatus(Constant.FALSE);
		writeResJsonStr(resp, CommonUtil.gson().toJson(ri));
	}
}
